package com.imay.live.fragment.room;

import android.os.Bundle;
import android.view.View;

import com.imay.live.manager.AccountManager;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.proto.helper.ProtoSocketFactory;
import com.imay.live.proto.user.UserInfoCard;
import com.imay.live.R;

import java.util.List;

/**
 * Created by jeaman on 2017/5/3.
 * 连麦列表
 */

public class LinkMicRequestFragment extends LinkMicInviteFragment {
    private final static int LINK_MIC_MAX_COUNT = 10;
    private boolean mHasApply;

    public static LinkMicRequestFragment newInstance(long roomId) {
        Bundle bundle = new Bundle();
        bundle.putLong(BUNDLE_NAME_ROOM_ID, roomId);
        LinkMicRequestFragment fragment = new LinkMicRequestFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_link_mic;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mNumberTv = $(R.id.link_mic_tv_number);
        mLinkBtn = $(R.id.link_mic_btn_link);

        mRecyclerView = $(R.id.link_mic_rv_user);
        initData();
    }

    @Override
    protected void setUserList(List<UserInfoCard> userList) {
        mHasApply = false;
        int number = 0;
        if (userList != null) {
            for (UserInfoCard userInfoCard : userList) {
                if (userInfoCard.uid == AccountManager.getInstance().getUid()) {
                    mHasApply = true;
                    break;
                }
            }
            number = userList.size();
        }
        if (number >= LINK_MIC_MAX_COUNT) {
            mLinkBtn.setEnabled(false);
        } else {
            mLinkBtn.setEnabled(true);
        }
        mNumberTv.setText(getString(R.string.link_mic_number, number));
        mLinkBtn.setText(mHasApply ? getString(R.string.link_mic_link_cancel) : getString(R.string.link_mic_link_apply));
        mAdapter.setNewData(userList);
    }

    @Override
    protected void setMeLinking(boolean meLinking) {
        if (meLinking) {
            mNumberTv.setText(getString(R.string.link_mic_linking));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.link_mic_btn_link:
                if (mHasApply) {
                    requestCancelLinkMic();
                } else {
                    requestLinkMic();
                }
                break;
        }
    }

    private void requestLinkMic() {
        ProtoRequestHelper.task(ProtoSocketFactory.requestLinkMic(mRoomId)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {

            }

            @Override
            public void onRequestError(String command, int code, String message) {
                if (isAdded() && !isDetached()) {
                    ErrorResult.toastError(getActivity(), code);
                }
            }
        });
    }

    private void requestCancelLinkMic() {
        ProtoRequestHelper.task(ProtoSocketFactory.requestCancelLinkMic(mRoomId)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {

            }

            @Override
            public void onRequestError(String command, int code, String message) {
                if (isAdded() && !isDetached()) {
                    ErrorResult.toastError(getActivity(), code);
                }
            }
        });
    }
}
